<template>
	<view class="container">
		<!-- 背景层 -->
		<!-- <view class="bg-layer" :style="{ backgroundImage: 'url(' + bgImage + ')' }"></view> -->
		<navgatsmyVue></navgatsmyVue>
		<!-- 导航栏 -->
		<view class="navbar">

			<view class="back-btn" @click="navigateBack" v-if="showDetail">
				<i class="fa fa-arrow-left"></i>
			</view>
			<view class="title">{{ showDetail ? currentCase.name : '福利宝箱' }}</view>
			<view class="logo">
				<i class="fa fa-diamond"></i>
			</view>
		</view>

		<!-- 宝箱列表视图 -->
		<view class="case-list" v-if="!showDetail">
			<view class="section-title">
				<text>可用福利宝箱</text>
				<view class="divider"></view>
			</view>

			<view class="cases-grid">
				<view class="case-item" v-for="(caseItem, index) in casesList" :key="caseItem.id"
					@click="showCaseDetail(caseItem)" :class="{ 'premium-case': caseItem.type === 3 }">
					<view class="case-image">
						<image :src="caseItem.box.cover" mode="aspectFit"></image>
					</view>
					<view class="case-info">
						<view class="case-name">{{ caseItem.name }}</view>
						<view class="case-desc">{{ caseItem.description || '无描述信息' }}</view>
						<view class="case-tag" :class="caseItem.type === 3 ? 'recharge-tag' : caseItem.type === 4 ? 'free-tag' : 'newbie-tag'">
							{{ caseItem.type_alias }}
							<template v-if="caseItem.promotion_level">
								{{ caseItem.promotion_level }}元
							</template>
						</view>
					</view>
					<!-- 已领完标识 -->
					<view class="case-exhausted" v-if="caseItem.remaining_count === 0">
						已领完
					</view>
				</view>
			</view>
		</view>

		<!-- 宝箱详情视图 -->
		<view class="case-detail" v-if="showDetail">
			<view class="case-header">
				<view class="case-big-image">
					<image :src="datasimage" mode="widthFix"></image>
				</view>
				<view class="case-main-info">
					<view class="case-title">{{ datasname }}</view>
					<view class="case-full-desc">{{ this.datasname || '无详细描述信息' }}</view>
					<view class="case-stats" v-if="currentCase.type === 3">
						<view class="stat-item">
							<i class="fa fa-rmb"></i>
							<span>充值金额: {{ currentCase.promotion_level }}元</span>
						</view>
					</view>
					<!-- 已领完提示 -->
					<view class="case-exhausted-tip" v-if="currentCase.remaining_count === 0">
						<i class="fa fa-exclamation-circle"></i>
						<span>该福利宝箱已领完</span>
					</view>
				</view>
			</view>

			<view class="section-title">
				<text>包含物品</text>
				<view class="divider"></view>
			</view>

			<!-- 横向排列的饰品列表 -->
			<view class="items-container">
				<view class="items-scroll">
					<view class="items-flex">
						<view class="item-card" v-for="(item, index) in currentCase" :key="item.id">
							<view class="item-rarity" :class="item.level === 1 ? 'rarity-common' : item.level === 2 ? 'rarity-rare' : item.level === 3 ? 'rarity-epic' : item.level === 4 ? 'rarity-legendary' : 'rarity-common'"></view>
							<view class="item-image">
								<image :src="item.skins.cover" mode="aspectFit"></image>
							</view>
							<view class="item-info">
								<view class="item-name">{{ item.skins.name }}</view>
								<view class="item-chance">概率: {{ item.odds_percent }}</view>
								<view class="item-price">价格: {{ item.skins.bean }}元</view>
							</view>
						</view>
					</view>
				</view>
			</view>

			<!-- CDK输入框 (非充值福利才显示) -->
			<view class="cdk-section" v-if="currentCase.type == 3">
				<view class="section-title">
					<text>兑换码</text>
					<view class="divider"></view>
				</view>
				<view class="cdk-input-group">
					<input type="text" placeholder="输入CDK兑换奖励" v-model="cdkCode" class="cdk-input"
						:disabled="currentCase.remaining_count === 0">
					<button class="cdk-verify-btn" @click="verifyCDK" :disabled="currentCase.remaining_count === 0">
						验证
					</button>
				</view>
				<view class="cdk-message" v-if="cdkMessage">{{ cdkMessage }}</view>
			</view>

			<!-- 抽奖按钮 -->
			<button class="draw-btn" @click="drawItem" :disabled="isDrawing || currentCase.remaining_count === 0">
				<i class="fa fa-gift" v-if="!isDrawing"></i>
				<i class="fa fa-spinner fa-spin" v-if="isDrawing"></i>
				<span>{{ currentCase.remaining_count === 0 ? '已领完' : (isDrawing ? '正在抽奖...' : '点击领取') }}</span>
			</button>
		</view>

		<!-- 抽奖结果弹窗 -->
		<view class="result-modal" v-if="showResult">
			<view class="modal-content">
				<view class="result-header">
					<text>恭喜获得</text>
					<view class="close-btn" @click="closeResult">
						<i class="fa fa-times"></i>
					</view>
				</view>
				<view class="result-item">
					<view class="item-rarity" :class="resultItem.level === 1 ? 'rarity-common' : resultItem.level === 2 ? 'rarity-rare' : resultItem.level === 3 ? 'rarity-epic' : resultItem.level === 4 ? 'rarity-legendary' : 'rarity-common'"></view>
					<view class="item-image">
						<image :src="resultItem.skins.image_url" mode="aspectFit"></image>
					</view>
					<view class="item-name">{{ resultItem.skins.name }}</view>
					<view class="item-price">价值: {{ resultItem.skins.bean }}元</view>
				</view>
				<button class="confirm-btn" @click="closeResult">
					确定
				</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		get_banners,
		getBoxDetail_det
	} from "../../api/outOfTheFree/index.js"
	import navgatsmyVue from "../../Component/navgatsmy/navgatsmy.vue";
	export default {
		data() {
			return {
				// 背景图片(实际项目中替换为用户提供的素材)
				bgImage: 'https://img.jingkakeji.com/img/static/bg.png',

				// 控制视图显示
				showDetail: false,
				currentCase: null,

				// CDK相关
				cdkCode: '',
				cdkMessage: '',

				// 抽奖状态
				isDrawing: false,
				showResult: false,
				resultItem: null,
				// 详情名
				datasname: "",
				datasimage: "",
				// 宝箱列表数据(使用提供的接口数据)
				casesList: [],
				id: 436
			};
		},
		components: {
			navgatsmyVue
		},
		onShow() {
			this.onlists()
		},
		methods: {
			async onlists() {
				let res = await get_banners()
				console.log(res)
				this.casesList = res.data
			},
			// 返回上一页
			navigateBack() {
				this.showDetail = false;
				this.currentCase = null;
				this.cdkCode = '';
				this.cdkMessage = '';
			},
			// 显示宝箱数据详情
			async showCaseDetail(caseItem) {
				console.log(caseItem, "id")
				let res = await getBoxDetail_det(caseItem.box_id || caseItem.id)
				// console.log(res,"详情数据")
				this.datasimage = res.data.cover
				this.datasname = res.data.name
				this.currentCase = res.data.contains; // 直接使用接口返回的data对象
				console.log(this.currentCase, "数据")
				this.showDetail = true;
				// 滚动到顶部
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 300
				});
			},

			// 根据类型获取标签样式
			getTagType(type) {
				switch (type) {
					case 3:
						return "recharge-tag"; // 充值福利
					case 4:
						return "free-tag"; // CDK箱子
					default:
						return "newbie-tag"; // 默认使用新手标签样式
				}
			},
			// 根据等级获取稀有度样式（已修正品质对应关系）
			getRarityClass(level) {
				switch (level) {
					case 1:
						return "rarity-common"; // 等级1对应普通
					case 2:
						return "rarity-rare"; // 等级2对应稀有
					case 3:
						return "rarity-epic"; // 等级3对应史诗
					case 4:
						return "rarity-legendary"; // 等级4对应传说
					default:
						return "rarity-common"; // 默认普通
				}
			},
			// 验证CDK
			verifyCDK() {
				if (!this.cdkCode.trim()) {
					this.cdkMessage = "请输入CDK";
					return;
				}

				// 模拟CDK验证
				this.cdkMessage = "验证成功！CDK有效";

				// 3秒后清除消息
				setTimeout(() => {
					this.cdkMessage = "";
				}, 3000);
			},

			// 抽奖/领取
			drawItem() {
				this.isDrawing = true;

				// 模拟抽奖过程
				setTimeout(() => {
					this.isDrawing = false;

					// 基于概率随机选择一个物品
					let totalChance = 0;
					const random = Math.random() * 100;

					for (const item of this.currentCase.items) {
						totalChance += item.chance;
						if (random <= totalChance) {
							this.resultItem = item;
							this.showResult = true;
							break;
						}
					}
				}, 2000);
			},

			// 关闭抽奖结果
			closeResult() {
				this.showResult = false;
				this.resultItem = null;
			}
		}
	};
</script>

<style scoped>
	.container {
		position: relative;
		min-height: 100vh;
		color: #fff;
		font-family: 'Helvetica Neue', Arial, sans-serif;
	}

	/* 背景层 */
	.bg-layer {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-size: cover;
		background-position: center;
		z-index: -2;
		filter: brightness(0.3);
	}

	/* 导航栏 */
	.navbar {
		display: flex;
		align-items: center;
		justify-content: center;
		/* margin-top: 100rpx; */
		padding: 16px 20px;
		background-color: rgba(15, 15, 15, 0.8);
		border-bottom: 1px solid rgba(255, 255, 255, 0.1);
		position: relative;
	}

	.back-btn {
		position: absolute;
		left: 20px;
		font-size: 20px;
		color: #fff;
	}

	.title {
		font-size: 18px;
		font-weight: bold;
	}

	.logo {
		position: absolute;
		right: 20px;
		font-size: 20px;
		color: #f9d342;
	}

	/* 列表视图样式 */
	.case-list {
		padding: 20px;
		background-color: black;
	}

	.section-title {
		margin-bottom: 15px;
		display: flex;
		align-items: center;
	}

	.section-title text {
		font-size: 16px;
		font-weight: bold;
		color: #f0f0f0;
	}

	.divider {
		flex: 1;
		height: 1px;
		background-color: rgba(255, 255, 255, 0.1);
		margin-left: 10px;
	}

	.cases-grid {
		display: grid;
		grid-template-columns: 1fr 1fr;
		gap: 15px;
		margin-bottom: 30px;
	}

	.case-item {
		background-color: rgba(30, 30, 30, 0.8);
		border-radius: 8px;
		overflow: hidden;
		border: 1px solid rgba(255, 255, 255, 0.1);
		transition: all 0.3s ease;
		position: relative;
	}

	.case-item:hover {
		transform: translateY(-5px);
		box-shadow: 0 10px 20px rgba(0, 0, 0, 0.3);
		border-color: rgba(255, 255, 255, 0.2);
	}

	/* 充值福利宝箱特殊样式 */
	.premium-case {
		border: 1px solid rgba(255, 215, 0, 0.5);
		background: linear-gradient(135deg, rgba(30, 30, 30, 0.9), rgba(50, 40, 0, 0.8));
	}

	.case-image {
		position: relative;
		height: 120px;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: rgba(0, 0, 0, 0.3);
	}

	.case-image image {
		width: 80px;
		height: 80px;
	}

	.case-info {
		padding: 10px;
	}

	.case-name {
		font-weight: bold;
		margin-bottom: 5px;
		font-size: 14px;
	}

	.case-desc {
		font-size: 12px;
		color: #b0b0b0;
		margin-bottom: 8px;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		overflow: hidden;
	}

	.case-tag {
		display: inline-block;
		padding: 2px 8px;
		border-radius: 4px;
		font-size: 11px;
	}

	.free-tag {
		background-color: #4CAF50;
		color: white;
	}

	.newbie-tag {
		background-color: #2196F3;
		color: white;
	}

	.recharge-tag {
		background-color: #f44336;
		color: white;
	}

	/* 已领完标识 */
	.case-exhausted {
		position: absolute;
		top: 10px;
		right: 10px;
		background-color: rgba(0, 0, 0, 0.7);
		color: #ff4444;
		font-size: 12px;
		padding: 2px 6px;
		border-radius: 3px;
		z-index: 1;
	}

	/* 详情视图样式 */
	.case-detail {
		padding: 20px;
	}

	.case-header {
		display: flex;
		margin-bottom: 30px;
		gap: 20px;
	}

	.case-big-image {
		width: 120px;
		height: 120px;
		flex-shrink: 0;
		background-color: rgba(0, 0, 0, 0.3);
		border-radius: 8px;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.case-big-image image {
		width: 100px;
		height: 100px;
	}

	.case-main-info {
		flex: 1;
	}

	.case-title {
		font-size: 20px;
		font-weight: bold;
		margin-bottom: 10px;
	}

	.case-full-desc {
		font-size: 14px;
		color: #b0b0b0;
		margin-bottom: 15px;
		line-height: 1.5;
	}

	.case-stats {
		display: flex;
		gap: 15px;
	}

	.stat-item {
		display: flex;
		align-items: center;
		gap: 5px;
		font-size: 13px;
		color: #ddd;
	}

	/* 已领完提示 */
	.case-exhausted-tip {
		display: flex;
		align-items: center;
		gap: 5px;
		font-size: 14px;
		color: #ff4444;
		margin-top: 10px;
	}

	/* 物品列表（横向排列） */
	.items-container {
		margin-bottom: 30px;
		width: 100%;
		overflow: hidden;
	}

	.items-scroll {
		width: 100%;
		overflow-x: auto;
		padding-bottom: 10px;
		/* 防止滚动条遮挡内容 */
		-webkit-overflow-scrolling: touch;
		/* 平滑滚动 */
	}

	.items-flex {
		display: flex;
		gap: 15px;
		min-width: max-content;
		/* 确保容器宽度足够容纳所有子元素 */
		padding: 5px 0;
	}

	.item-card {
		width: 140px;
		/* 固定卡片宽度 */
		background-color: rgba(30, 30, 30, 0.8);
		border-radius: 6px;
		padding: 10px;
		border: 1px solid rgba(255, 255, 255, 0.1);
		box-sizing: border-box;
		flex-shrink: 0;
		/* 防止卡片被压缩 */
	}

	/* 响应式调整卡片宽度 */
	@media (max-width: 375px) {
		.item-card {
			width: 120px;
		}
	}

	.item-rarity {
		height: 3px;
		margin-bottom: 8px;
		border-radius: 3px;
	}

	/* 品质颜色 */
	.rarity-common {
		background-color: #b0b0b0;
		/* 普通 - 灰色 */
	}

	.rarity-rare {
		background-color: #14b866;
		/* 稀有 - 绿色 */
	}

	.rarity-epic {
		background-color: #6d28d9;
		/* 史诗 - 紫色 */
	}

	.rarity-legendary {
		background-color: #d97706;
		/* 传说 - 橙色 */
	}

	.item-image {
		height: 60px;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-bottom: 8px;
	}

	.item-image image {
		width: 50px;
		height: 50px;
	}

	.item-info {
		text-align: center;
	}

	.item-name {
		font-size: 12px;
		font-weight: 500;
		margin-bottom: 3px;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.item-chance {
		font-size: 11px;
		color: #b0b0b0;
	}

	/* CDK部分 */
	.cdk-section {
		margin-bottom: 30px;
	}

	.cdk-input-group {
		display: flex;
		gap: 10px;
	}

	.cdk-input {
		flex: 1;
		padding: 10px 15px;
		background-color: rgba(20, 20, 20, 0.8);
		border: 1px solid rgba(255, 255, 255, 0.1);
		border-radius: 4px;
		color: #fff;
		font-size: 14px;
	}

	.cdk-input:disabled {
		background-color: rgba(50, 50, 50, 0.5);
		color: #888;
	}

	.cdk-verify-btn {
		padding: 0 15px;
		background-color: #2196F3;
		color: white;
		border: none;
		border-radius: 4px;
		font-size: 14px;
	}

	.cdk-verify-btn:disabled {
		background-color: #555;
		opacity: 0.7;
	}

	.cdk-message {
		margin-top: 8px;
		font-size: 13px;
		color: #4CAF50;
	}

	/* 抽奖按钮 */
	.draw-btn {
		width: 100%;
		padding: 15px;
		background-color: #f44336;
		color: white;
		border: none;
		border-radius: 6px;
		font-size: 16px;
		font-weight: bold;
		display: flex;
		align-items: center;
		justify-content: center;
		gap: 8px;
		margin-bottom: 30px;
	}

	.draw-btn:disabled {
		background-color: #555;
		opacity: 0.8;
	}

	/* 结果弹窗 */
	.result-modal {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.8);
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 100;
	}

	.modal-content {
		width: 85%;
		max-width: 350px;
		background-color: #1a1a1a;
		border-radius: 10px;
		padding: 20px;
		border: 2px solid #f9d342;
	}

	.result-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20px;
	}

	.result-header text {
		font-size: 18px;
		font-weight: bold;
		color: #f9d342;
	}

	.close-btn {
		font-size: 20px;
		color: #b0b0b0;
	}

	.result-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 25px;
	}

	.result-item .item-image {
		height: 120px;
		margin-bottom: 15px;
	}

	.result-item .item-image image {
		width: 100px;
		height: 100px;
	}

	.result-item .item-name {
		font-size: 18px;
		font-weight: bold;
		margin-top: 10px;
	}

	.confirm-btn {
		width: 100%;
		padding: 12px;
		background-color: #f9d342;
		color: #1a1a1a;
		border: none;
		border-radius: 6px;
		font-size: 16px;
		font-weight: bold;
	}

	.item-price {
		font-size: 11px;
		color: #4CAF50;
		margin-top: 2px;
	}

	.result-item .item-price {
		font-size: 14px;
		margin-top: 8px;
		color: #f9d342;
	}
</style>